package cc.sjfx.sharedoc.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CheckUtils {
	public static Boolean stringIsNull(String... s) {
		for (String string : s) {
			if (string == null) {
				return true;
			} else if (string.trim().equals("")) {
				return true;
			} else if (string.trim().toLowerCase().equals("null")) {
				return true;
			}
		}
		return false;
	}

	public static boolean phoneIsValid(String phone) {
		if (phone.contains("@")) {
			String check = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(phone);
			return matcher.matches();
		} else {
			if (phone.length() == 11) {
				try {
					Long.parseLong(phone);
					if (phone.startsWith("1")) {
						return true;
					}
				} catch (Exception e) {
					// TODO: handle exception
					return false;
				}
			}
		}
		return false;
	}

	public static boolean usernameIsEmail(String username) {
		if (username.contains("@")) {
			return true;
		}
		return false;
	}

	public static boolean passIsValid(String password) {
		if (password.length() >= 6 && password.length() <= 20) {
			return true;
		}
		return false;
	}

	public static int tableName(String table) {
		for (int i = 0; i < StrUtils.TABLE.length; i++) {
			if (table.equals(StrUtils.TABLE[i])) {
				return i;
			}
		}
		return -1;
	}

	public static boolean isWebSite(String s) {
		Pattern p = Pattern
				.compile("http://(([a-zA-z0-9]|-){1,}\\.){1,}[a-zA-z0-9]{1,}-*");
		Matcher m = p.matcher(s);
		return m.find();
	}
}
